package com.example.wishstar.repository;

import android.util.Log;

import com.example.wishstar.api.Api;
import com.example.wishstar.model.NewHabit;
import com.example.wishstar.utils.ApiResponse;
import com.google.gson.JsonObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddHabitRepository {
    private String TAG = "AddHabitRepository";
    private Api api;

    public AddHabitRepository(Api api) {
        this.api = api;
    }

    public void addHabit(NewHabit newHabit) {
        JsonObject fieldMap = new JsonObject();
        fieldMap.addProperty("userId", newHabit.userId);
        fieldMap.addProperty("habitTitle", newHabit.habitTitle);
        fieldMap.addProperty("habitIcon", newHabit.habitIcon);
        fieldMap.addProperty("frequency", newHabit.frequency);

        api.addHabit(RequestBody.create(MediaType.parse("application/json"), fieldMap.toString())).enqueue(new Callback<ApiResponse<Integer>>() {
            @Override
            public void onResponse(Call<ApiResponse<Integer>> call, Response<ApiResponse<Integer>> response) {
                Log.e(TAG, "addHabit->onResponse");
                if (response.body() != null) {
                    if (response.body().code == 200) {
                        Log.e(TAG, "AddHabit成功");
                    }
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Integer>> call, Throwable t) {
                Log.e(TAG, "addHabit->onFailure");
            }
        });
    }
}
